﻿define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Credit_repayment_list, $stateParams, $ionicSlideBoxDelegate, $ionicModal, $timeout,GoDialog) {
		
		$scope.store_id = $stateParams.store_id;
		$scope.selectIndex = 0;
		$scope.datasArr = new Array();//存放一览数据
		$scope.nPageArr = [1,1];//页数
		$scope.type = "pay";//默认显示欠款明细
		
		$scope.$on('$ionicView.afterEnter', function() {
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				lan = 20;
			}
			var all = window.innerHeight;
			$scope.myObj = {
				"height": all - 44*2 - lan + "px"
			}
			$scope.init_data();
		}, false);
		
		//切换页面
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide($scope.selectIndex);
			if($scope.selectIndex==0){
				$scope.type = "pay";//欠款：pay
			}else{
				$scope.type = "repay";//还款：repay
			}
			if(!$scope.datasArr[$scope.selectIndex]){
				$scope.init_data();
			}
		};
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};

		//初始化页面。还款：repay      欠款：pay
		$scope.init_data = function() {
			Credit_repayment_list.credit_list($scope.store_id, 1,$scope.type, function(data) {
				$scope.datasArr[$scope.selectIndex] = data;
			})
		}
		
		/***************查看凭证**************/
		$ionicModal.fromTemplateUrl("templates/modal/credit_voucher.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_voucher = modal;
		});
		//打开
		$scope.openModal_voucher = function(index) {
			var data = $scope.datasArr[1].credit[index];
			if(!data) return;
			var repayment = data.repayment;
			$scope.voucher = data.voucher;
			$scope.voucher.repayment = repayment;
			$scope.modal_voucher.show();
		}
		//隐藏
		$scope.closeModal_voucher = function() {
			$scope.modal_voucher.hide();
		}
		/***************查看凭证**************/
		
		/*===================下拉刷新 =============*/
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Credit_repayment_list.credit_list($scope.store_id, 1,$scope.type, function(data) {
				$scope.datasArr[$scope.selectIndex] = data;
				$scope.nPageArr[$scope.selectIndex] = 1;
			},function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Credit_repayment_list.credit_list($scope.store_id, $scope.nPageArr[$scope.selectIndex] + 1, $scope.type, function(data) {
//				console.log(data.credit)
					if(data != null) {
						if($scope.datasArr[$scope.selectIndex] != undefined) {
							$scope.datasArr[$scope.selectIndex].credit = $scope.datasArr[$scope.selectIndex].credit.concat(data.credit);
							$scope.nPageArr[$scope.selectIndex] += 1;
//							console.log($scope.nPageArr)
						} else {
							$scope.datasArr[$scope.selectIndex] = data;
						}
					}
				},function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		}
		/*==================下拉刷新 =============*/
		
		//图片放大
		$scope.showBigImg = function(imgUrl) {
			$scope.closeModal_voucher();//必须关闭弹窗，要不然体验很差
			GoDialog.showBigImg(imgUrl);
		}
		
		
		
	}

	controller.$inject = ['$scope', 'Credit_repayment_list', '$stateParams','$ionicSlideBoxDelegate','$ionicModal','$timeout','GoDialog'];

	return app.registerController("credit_repayment_listctrl", controller);
});